<?php
require_once 'config.php';

/*********************************************************
*                 SHOPPING CART FUNCTIONS 
*********************************************************/

$action = (isset($_GET['action']) && $_GET['action'] !='') ? $_GET['action'] : 'view';
switch ($action) {
	case 'add' :
		addToCart();
		break;
	case 'update' :
		updateCart();
		break;
	case 'delete' :
		deleteItemCart();
		break;
	case 'view' :
}

function addToCart()
{
if(!empty($_POST['txtQty'])) {
		 $txtQty_f = $_POST['txtQty'];
	} 
		else
	{
		$txtQty_f = 1;
	}
	// make sure the product id exist
	if(isset($_GET['pid']) && (int)$_GET['pid'] > 0){
		$productId = (int)$_GET['pid'];
	} else {
		header ('location: index.php');
	}
	// current session id
	$sid = session_id();
	
	// check if the product is already
	// in cart table for this session
	$sql = "SELECT pd_id
			FROM tbl_cart
			WHERE pd_id = $productId AND ct_session_id = '$sid'";
	$result = mysql_query($sql);
	
	if (dbNumRows($result) == 0){
		// put the product in cart table
		$sql_i = "INSERT INTO tbl_cart (pd_id, ct_qty, ct_session_id, ct_date)
				VALUES ($productId, $txtQty_f, '$sid', NOW())";	
		$result_i = mysql_query($sql_i);
	} else {
		// update product quantity in cart table

		$sql_u = "UPDATE tbl_cart
				SET ct_qty = ct_qty + $txtQty_f
				WHERE pd_id = $productId AND ct_session_id = '$sid'";
		//echo $sql_u; exit;
		$result_u = mysql_query($sql_u);
	}
	deleteAbandonedCart();
	header('Location: '.W_ROOT.'/cart.php');
}

/*
	Remove an item from the cart
*/
function deleteItemCart($cartId = 0)
{
	if (!$cartId && isset($_GET['cid']) && (int)$_GET['cid'] > 0) {
		$cartId = (int)$_GET['cid'];
	}

	if ($cartId) {	
		$sql  = "DELETE FROM tbl_cart
				 WHERE ct_id = $cartId";

		$result = mysql_query($sql);
	}
	
	header('Location: cart.php');	
}
/*
	Update item quantity in shopping cart
*/
function updateCart()
{
	$cartId     = $_POST['hidCartId'];
	$itemQty    = $_POST['txtQty'];
	$numItem    = count($itemQty);
	$numDeleted = 0;
	$notice     = '';
	
	for ($i = 0; $i < $numItem; $i++) {
		$newQty = (int)$itemQty[$i];
		if ($newQty < 1) {
			// remove this item from shopping cart
			deleteItemCart($cartId[$i]);	
			$numDeleted += 1;
		} else {
							
			// update product quantity
			$sql = "UPDATE tbl_cart
					SET ct_qty = $newQty
					WHERE ct_id = {$cartId[$i]}";
				
			mysql_query($sql);
		}
	}
	
	if ($numDeleted == $numItem) {
		// if all item deleted return to the last page that
		// the customer visited before going to shopping cart
		header('Location: cart.php');
	} else {
		header('Location: cart.php');	
	}
	
	exit;
}

function isCartEmpty()
{
	$isEmpty = false;
	
	$sid = session_id();
	$sql = "SELECT ct_id
			FROM tbl_cart ct
			WHERE ct_session_id = '$sid'";
	
	$result = mysql_query($sql);
	
	if (dbNumRows($result) == 0) {
		$isEmpty = true;
	}	
	
	return $isEmpty;
}

/*
	Delete all cart entries older than one day
*/
function deleteAbandonedCart()
{
	$yesterday = date('Y-m-d H:i:s', mktime(0,0,0, date('m'), date('d') - 1, date('Y')));
	$sql = "DELETE FROM tbl_cart
	        WHERE ct_date < '$yesterday'";
	mysql_query($sql);		
}
?>